package com.suixun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.NewsCenterMapper;
import com.suixun.dao.redis.RedisDao;
import com.suixun.domain.NewsCenter;
import com.suixun.enums.ShowEnum;
import com.suixun.exceptions.SuixunException;
import com.suixun.service.NewsCenterService;
import com.suixun.utils.ListUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 新闻中心 服务实现类
 * </p>
 *
 * @author jacky
 * @since 2018-01-11
 */
@Service
public class NewsCenterServiceImpl extends ServiceImpl<NewsCenterMapper, NewsCenter> implements NewsCenterService {

    @Resource
    private NewsCenterMapper newsCenterMapper;
    @Value("${hot.news.num}")
    private Integer hotNewsNum;
    @Resource
    private RedisDao redisDao;

    @Override
    @Cacheable(value = "newsCenterCache", key = "'newsCenter_pageNewsCenter' + '_' + #page + '_' + #size + '_' + #type")
    public PageInfo<NewsCenter> pageNewsCenter(int page, int size, Integer type) {
        // 类型为空时，为默认
        if (type == null) {
            type = 1;
        }
        PageHelper.startPage(page, size);
        Wrapper<NewsCenter> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type).orderBy("top", false);
        return new PageInfo<NewsCenter>(this.selectList(wrapper));
    }

    @Override
    @Cacheable(value = "newsCenterCache", key = "'newsCenter_pageNewsCenterExclusionContent' + '_' + #page + '_' + #size + '_' + #title + '_' + #type + '_' + #isShow")
    public PageInfo<NewsCenter> pageNewsCenterExclusionContent(int page, int size, String title, Integer type, Integer isShow) {
        // 类型为空时，为默认
        if (type == null) {
            type = 1;
        }
        PageHelper.startPage(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("title", title);
        map.put("isShow", isShow);
        return new PageInfo<NewsCenter>(newsCenterMapper.listNewsCenterExclusionContent(map));
    }

    @Override
    @Cacheable(value = "newsCenterCache", key = "'newsCenter_listHotNewCenter' + '_' + #title")
    public List<NewsCenter> listHotNewCenter(String title) {
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("hotNewsNum", hotNewsNum);
        return newsCenterMapper.listHotNewsCenter(map);
    }

    @Override
    @Cacheable(value = "newsCenterCache", key = "'newsCenter_getById' + '_' + #id")
    public NewsCenter getById(Integer id) {
        return this.selectById(id);
    }

    @Override
    public void insert(NewsCenter newsCenter, Boolean isTop) {
        checkShow(newsCenter.getIsShow());
        if (isTop != null && isTop) {
            newsCenter.setTop(String.valueOf(new Date().getTime()));
        }
        this.insert(newsCenter);
        // 清缓存
        redisDao.flushCaches("newsCenter*");
    }

    private void checkShow(Integer show) {
        if (show != null && !ShowEnum.containType(show)) {
            throw new SuixunException("展示数据不符");
        }
    }

    @Override
    public void update(NewsCenter newsCenter, Boolean isTop) {
        checkShow(newsCenter.getIsShow());
        if (newsCenter.getId() == null) {
            throw new SuixunException("id不能为空");
        }
        Date date = new Date();
        if (isTop != null) {
            if (isTop) {
                newsCenter.setTop(String.valueOf(date.getTime()));
            } else {
                newsCenter.setTop("0");
            }
        }
        this.updateById(newsCenter);
        // 清缓存
        redisDao.flushCaches("newsCenter*");
    }

    @Override
    public void delete(String ids) {
        this.deleteBatchIds(ListUtil.Integers2List(ids));
        // 清缓存
        redisDao.flushCaches("newsCenter*");
    }

    @Override
    public void updateReadingNum(Integer id) {
        // 查询
        NewsCenter newsCenter = this.selectById(id);
        if (newsCenter == null) {
            throw new SuixunException("新闻不存在");
        }
        int readNum = newsCenter.getReadNum() + 1;
        newsCenter = new NewsCenter();
        newsCenter.setId(id);
        newsCenter.setReadNum(readNum);
        this.updateById(newsCenter);
        // 清缓存
        redisDao.flushCaches(String.format("newsCenter_getReadNumById_%d", id));
        redisDao.flushCaches(String.format("newsCenter_getNewsCenter_%d", id));
    }

    @Override
    @Cacheable(value = "newsCenterCache", key = "'newsCenter_getReadNumById' + '_' + #id")
    public Integer getReadNumById(Integer id) {
        NewsCenter newsCenter = this.selectById(id);
        if (newsCenter != null) {
            return newsCenter.getReadNum();
        }
        return null;
    }
}
